﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Common;
using DotNetCSharp;

namespace WinFormTest
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {


            int[] index = new int[300];
            int total = 0;
                System.IO.StreamWriter file = new System.IO.StreamWriter("c:\\test.txt");
            for (int x = 0; x < 100; x++)
            {
                List<double> list = new List<double>();       
                ulong pocket = HandEvaluator.RandomHand(0UL, 2);
                ulong board = HandEvaluator.RandomHand(pocket, 4);
                double odds = HandEvaluator.GetWinOdds(pocket, board, 50000);
                for (int i = 0; i < 1000; i++)
                {
                    ulong opp = HandEvaluator.RandomHand(board, 2);
                    double wodds = HandEvaluator.GetWinOdds(opp, board, 1000);
                    list.Add(wodds);
                }
                list.Sort();
               total += list.FindIndex(o => o > odds);

                //file.WriteLine("Pocket:" + ParseMask(pocket) + " Board:" + ParseMask(board) + " win:" + odds + "rank:" + index[x]);

                //for (int y = 0; y < 2; y++)
                //{
                //    list.Clear();
                //    ulong boardR = HandEvaluator.RandomHand(pocket | board, 1);
                //    board = board | boardR;
                //    odds = HandEvaluator.GetWinOdds(pocket, board, 50000);
                //    for (int i = 0; i < 1000; i++)
                //    {
                //        ulong opp = HandEvaluator.RandomHand(board, 2);
                //        double wodds = HandEvaluator.GetWinOdds(opp, board, 1000);
                //        list.Add(wodds);
                //    }
                //    list.Sort();
                //    index[x] = list.FindIndex(o => o > odds);

                //    file.WriteLine("Pocket:" + ParseMask(pocket) + " Board:" + ParseMask(board) + " win:" + odds + "rank:" + index[x]);
                //}

            }

                file.Close();

        }
        private bool GetSceneStat(List<int> list, Func<int, bool> scene, Func<int, bool> hit, int minScenes, double threshould)
        {
            var hands = list;
            hands = hands.Where(scene).ToList();
            double numHits;
            while (hands.Count > minScenes * 2)
            {
                hands = hands.GetRange(0, hands.Count / 2);
            }
            if (hands.Count < minScenes)
            {
                return false;
            }
            else
            {
                numHits = hands.Where(hit).Count();
                return numHits > threshould * hands.Count;
            }
        }

        private void btnExec_Click(object sender, EventArgs e)
        {
           
        }
        private string ParseMask(ulong mask)
        {
            string result = string.Empty;
            for (int i = 0; i < 52; i++)
            {
                ulong m = 1UL << i;
                ulong binaryPlus = mask & m;
                if (binaryPlus > 0)
                {
                    int suit = i / 13;
                    int rank = i % 13;
                    switch (rank)
                    {
                        case 0:
                            result +="2";
                            break;
                        case 1:
                            result += "3";
                            break;
                        case 2:
                            result += "4";
                            break;
                        case 3:
                            result += "5";
                            break;
                        case 4:
                            result += "6";
                            break;
                        case 5:
                            result += "7";
                            break;
                        case 6:
                            result += "8";
                            break;
                        case 7:
                            result += "9";
                            break;
                        case 8:
                            result += "T";
                            break;
                        case 9:
                            result += "J";
                            break;
                        case 10:
                            result += "Q";
                            break;
                        case 11:
                            result += "K";
                            break;
                        case 12:
                            result += "A";
                            break;
                        default:
                            break;
                    }
                    switch(suit)
                    {
                        case 0:
                            result += "c";
                            break;
                        case 1:
                            result += "d";
                            break;
                        case 2:
                            result += "h";
                            break;
                        case 3:
                            result += "s";
                            break;
                        default:
                            break;
                    }

                }
            }
            return result;
        }
    }
    
}
